############################################################
# Makefile for the regression tests that are run on travis
############################################################
# $(TRAVIS_BUILD_DIR) is /home/travis/build/lbl-srg/modelica-buildings
ROOT = $(TRAVIS_BUILD_DIR)

test-documentation:
	(cd $(ROOT)/Buildings; ../bin/runUnitTests.py --validate-html-only)
	(cd $(ROOT)/Buildings/Resources/Documentation/userGuide; make regressiontest)
	(cd $(ROOT)/Buildings/Resources/src/fluid/heatpumps/calibration/doc; make regressiontest)

test-experiment-setup:
	(cd $(ROOT)/Buildings; ../bin/runUnitTests.py --validate-experiment-setup)

test-autogenerated-files:
	@echo "*********************************************************"
	@echo "*** Check for package.order to be up to date"
	(cd  $(ROOT)/Buildings; \
	  python -c 'import buildingspy.development.refactor as r; \
	  r.write_package_order(".", recursive=True)'; \
	  git diff --exit-code . )

	@echo "*********************************************************"
	@echo "*** Verify that all OpenModelica scripts are up to date, and no old scripts exist"
	(cd  $(ROOT)/Buildings && \
	  rm Resources/Scripts/OpenModelica/compareVars/*.mos && \
	  python -c 'import buildingspy.development.regressiontest as u; \
	  u.Tester().writeOpenModelicaResultDictionary()')
	git status --porcelain $(ROOT)/Buildings/Resources/Scripts/OpenModelica;
	test -z "$$(git status --porcelain $(ROOT)/Buildings/Resources/Scripts/OpenModelica)"

	@echo "*********************************************************"
	@echo "*** Check for UnitConversion package to be consistent with the script that generates it"
	(cd  $(ROOT); \
	  python Buildings/Resources/src/Controls/OBC/UnitConversions/unit_converters.py; \
	  git diff --exit-code Buildings)

test-verify-files:
	(cd $(ROOT)/Buildings && ../bin/verifyFiles.py)

# Test whether all EnergyPlus validation files are up to date.
# This targets searches all files `run.sh` and executes them.
test-energyplus-validations:
	@find $(ROOT)/Buildings/Resources/Data -name run.sh | xargs -I {} sh -c \
  	  'cd `dirname {}` && \
	   echo "*********************************************************" && \
	   echo "*** Testing `pwd`/run.sh" && \
	   ./run.sh'
	@echo "*** Verify that all files in repository are up to date"
	git diff --exit-code $(ROOT)/Buildings/Resources/Data

test-cdl-conformance:
	(cd $(ROOT) && \
	 echo "*********************************************************" && \
	 echo "*** Running modelica-json" && \
 	 export MODELICAPATH=`pwd` && \
	 node $(MODELICA_JSON_HOME)/app.js -f Buildings/Controls/OBC -o json -m modelica -d out --strict true && \
	 rm -rf out)

test-dymola:
	(cd $(ROOT)/Buildings && \
	  export PYTHONPATH=$(TRAVIS_BUILD_DIR)/Buildings/Resources/Python-Sources && \
	  python3 ../bin/runUnitTests.py --batch --single-package $(PACKAGE) --tool dymola)

test-optimica:
	(cd $(ROOT)/Buildings && \
	  export PYTHONPATH=$(TRAVIS_BUILD_DIR)/Buildings/Resources/Python-Sources && \
	  python3 ../bin/runUnitTests.py --batch --single-package $(PACKAGE) --tool optimica --skip-verification)

test-jmodelica:
	(cd $(ROOT)/Buildings && \
	  export PYTHONPATH=$(TRAVIS_BUILD_DIR)/Buildings/Resources/Python-Sources && \
	  python3 ../bin/runUnitTests.py --batch --single-package $(PACKAGE) --tool jmodelica --skip-verification)
